package com.Lixiaoqian.CardPlay.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.Lixiaoqian.CardPlay.R;
import com.Lixiaoqian.CardPlay.base.BaseActivity;
import com.Lixiaoqian.CardPlay.utils.DialogUtils;

import butterknife.BindView;

import static com.umeng.socialize.utils.DeviceConfig.context;

public class SuggestionActivity extends BaseActivity {

    @BindView(R.id.tv_topbar_center)
    TextView tvTopbarCenter;
    @BindView(R.id.iv_topbar_right)
    ImageView ivTopbarRight;
    @BindView(R.id.toptoolbar)
    Toolbar toptoolbar;
    @BindView(R.id.editText_message)
    EditText editTextMessage;
    @BindView(R.id.layout_message)
    LinearLayout layoutMessage;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.layout_feeback)
    LinearLayout layoutFeeback;
    @BindView(R.id.editText_name)
    EditText editTextName;
    @BindView(R.id.layout_name)
    LinearLayout layoutName;
    @BindView(R.id.btn_sure)
    Button btnSure;
    int num = 500;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initEvent();
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_suggestion;
    }

    @Override
    protected void initToolbar() {
        super.initToolbar();
        actionBar.setTitle("反馈");
    }

    public static void launch(Activity fromactivity) {
        fromactivity.startActivity(new Intent(fromactivity, SuggestionActivity.class));
    }

    private void initEvent() {
        editTextMessage.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                temp = s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                int number = s.length();
                tvNum.setText("" + number + "/500");
                selectionStart = editTextMessage.getSelectionStart();
                selectionEnd = editTextMessage.getSelectionEnd();
                // System.out.println("start="+selectionStart+",end="+selectionEnd);
                if (temp.length() > num) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionStart;
                    editTextMessage.setText(s);
                    editTextMessage.setSelection(tempSelection);// 设置光标在最后
                }
            }

        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (editTextMessage.length() == 0) {
                    finish();
                } else {
                    DialogUtils.showNotitleDialog(this, "确定放弃反馈?", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    });
                }
        }
        return true;
    }
}
